from __future__ import annotations

from dataclasses import dataclass
from math import pi


@dataclass
class PileAnchorCapacity:
    uplift_kN: float
    overturning_kNm: float
    note: str


def pile_anchor_capacity(diameter_m: float, length_m: float, skin_friction_kPa: float, end_bearing_kPa: float) -> PileAnchorCapacity:
    """桩式地锚抗拔/抗倾覆简算：
    - 抗拔≈周摩阻+端承：U = π·d·l·τ + (π d^2/4)·q
    - 抗倾覆矩取 U×(l/2)
    """

    import math
    U = math.pi * diameter_m * length_m * (skin_friction_kPa / 1000.0) + (math.pi * (diameter_m ** 2) / 4.0) * (end_bearing_kPa / 1000.0)
    M = U * (length_m / 2.0)
    return PileAnchorCapacity(uplift_kN=U, overturning_kNm=M, note="桩锚承载估算")


@dataclass
class PitAnchorCapacity:
    uplift_kN: float
    note: str


def pit_anchor_capacity(weight_kN: float, soil_bearing_kPa: float, base_area_m2: float, friction_coeff: float = 0.5) -> PitAnchorCapacity:
    """坑式（重力式）地锚：抗拔≈自重+底面摩擦，受地基承载力限制。"""

    capacity_bearing = soil_bearing_kPa / 1000.0 * base_area_m2
    friction = weight_kN * friction_coeff
    U = min(weight_kN + friction, capacity_bearing)
    return PitAnchorCapacity(uplift_kN=U, note="以承载力为上限")


